var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({
  data: {
    showType: 0,
    page:1,
    size:8,
    count:0,
    allPage:1,
    commentList:[],
    showTips:1
  },
  // 跳转写评论界面
  toWriteRemark(e){
    let productInfo=JSON.stringify(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '../write-remark/write-remark?info='+productInfo,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  switchTab: function (event) {
    let showType = event.currentTarget.dataset.index;
    this.setData({
      showType: showType,
      page:1,
      size:8,
      count:0,
      allPage:1,
      commentList:[],
      showTips:1
    });
    wx.showLoading({
      title: '加载中',
    });
    this.getUserComment();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    });
    this.setData({
      page:1,
      size:8,
      count:0,
      allPage:1,
      commentList:[],
      showTips:1
    });
    this.getUserComment();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      let that = this;
      if (that.data.page >= that.data.allPage) {
          that.setData({
              showTips: 1
          });
          return false;
      }
      wx.showLoading({
        title: '加载中',
      })
      that.setData({
          page: that.data.page + 1
      });
      that.getUserComment();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取可评论商品的列表
  getUserComment(){
    let that=this;
    let userID=wx.getStorageSync('userID');
    util.requestApi(api.userComments, {
        userID: userID,
        isComment:that.data.showType,
        page:1,
        size:that.data.size
    }).then(function (res) {
        if (res.code == 200) {
          that.setData({
            count:res.content.count,
            commentList:that.data.commentList.concat(res.content.data),
            allPage: Math.ceil(res.content.count / that.data.size),
            showTips: res.content.data.length < that.data.size ? 1 : 0,
          });
          wx.hideLoading();
        }else{
          wx.showToast({
            title: '获取评论列表失败',
            icon:'none',
            duration:2000
          });
        }
    })
  },
  // 跳转到商品界面
  goProduct(e){
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/goods/goods?id='+id,
    });
  }
})